package com.hzpd.hainan.module.zhuangti;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hzpd.hainan.R;
import com.hzpd.hainan.api.InterfaceJsonfile;
import com.hzpd.hainan.app.BaseFragment;
import com.hzpd.hainan.model.news.NewsBean;
import com.hzpd.hainan.model.zhuanti.ZhuanTiBean;
import com.hzpd.hainan.model.zhuanti.ZhuanTiEntity;
import com.hzpd.hainan.module.news.activity.NewsZhuanTiActivity;
import com.hzpd.hainan.module.zhuangti.adapter.ZhuanTiAdapter;
import com.hzpd.hainan.net.Factory;
import com.hzpd.hainan.utils.AAnim;
import com.hzpd.hainan.utils.LogUtils;
import com.hzpd.hainan.utils.ParamUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/8/9.
 */

public class ZhuanTifragment extends BaseFragment implements BaseQuickAdapter.OnItemChildClickListener,BaseQuickAdapter.RequestLoadMoreListener{
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout swipe_layout;
    @BindView(R.id.recycler_zhuanti)
    RecyclerView recyclerView;
    private List<ZhuanTiBean> list;
    private ZhuanTiAdapter adapter;
    private int Page = 1;
    private int Pagesize = 15;
    boolean mFlagRefresh;

    @Override
    public int setMyContentView() {
        return R.layout.fragment_zhuanti;
    }

    @Override
    public void initView() {

    }

    public static ZhuanTifragment newInstance() {
        ZhuanTifragment zhuantiFragment = new ZhuanTifragment();
        return zhuantiFragment;
    }

    @Override
    public void initData() {
        list = new ArrayList<>();
        adapter = new ZhuanTiAdapter(activity,list);
        LinearLayoutManager manager = new LinearLayoutManager(activity);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        initList();
        swipe_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Pagesize = 1;
                mFlagRefresh = true;
                initList();

            }
        });
        adapter.setOnItemChildClickListener(this);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ItemClick(adapter, view, position);
            }
        });
    }

    private void ItemClick(BaseQuickAdapter adapter, View view, int position) {
        ZhuanTiBean obj = (ZhuanTiBean) adapter.getItem(position);
        Intent intent = new Intent(activity, NewsZhuanTiActivity.class);
        NewsBean nb = new NewsBean();
        nb.setNid(obj.getNid());
        intent.putExtra("nb", nb);
        intent.putExtra("from", "subject");
        startActivity(intent);
        AAnim.ActivityStartAnimation(activity);
    }

    private void initList() {
        Map<String, String> map = new HashMap<>();
        map.put("siteid", InterfaceJsonfile.SITEID);
        map.put("Page", Page + "");
        map.put("PageSize", Pagesize + "");
        Factory.provideHttpService().getSubjectList(ParamUtils.getRequestParam(map))
                .onBackpressureBuffer()
                .subscribeOn(Schedulers.io())
                .filter(new Func1<ZhuanTiEntity, Boolean>() {
                    @Override
                    public Boolean call(ZhuanTiEntity notifiEntity) {
                        return true;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ZhuanTiEntity>() {
                    @Override
                    public void call(ZhuanTiEntity zhuantiEntity) {
                        LogUtils.e("code--" + zhuantiEntity.code);
                        if (mFlagRefresh) {
                            swipe_layout.setRefreshing(false);
                        }

                        if ("200".equals(zhuantiEntity.code)) {

                            setListData(zhuantiEntity.data);
                            adapter.notifyDataSetChanged();
                            swipe_layout.setRefreshing(false);
                            LogUtils.e(list.size() + "");
                        } else if ("209".equals(zhuantiEntity.code)) {

                            list.clear();
                            adapter.notifyDataSetChanged();
                        } else {
                            if (!mFlagRefresh) {
                                Page--;
                            }
                        }
                        mFlagRefresh = false;
                    }

                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtils.i("throwable--" + throwable.getMessage());
                    }
                });


    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        ItemClick(adapter, view, position);
    }

    public void setListData(List<ZhuanTiBean> list) {
        if (mFlagRefresh) {
            adapter.setNewData(list);
            adapter.notifyDataSetChanged();
        } else {
            adapter.addData(list);
            adapter.notifyDataSetChanged();
            adapter.loadMoreComplete();
        }
        LogUtils.i("size-->" + adapter.getData().size());
        if (list.size() < Pagesize) {
            //关闭刷新
            adapter.loadMoreEnd(true);
        }
    }

    @Override
    public void onLoadMoreRequested() {
        Page++;
        LogUtils.e("Page"+Page);
        initList();
    }
}
